import { configByID, gameConfig, gameConfigHandle } from "js/config/gameConfig"
import Random from "js/core/random"
import { ItemModel } from "js/model/itemModel"
import { EquipmentsModel, PartnerModel } from "js/model/partnerModel"
import { PlayerModel } from "js/model/playerModel"
import { ConditionType, CreateKind } from "js/typeDefine/typeDefine"
import equipmentHandle from "./equipmentHandle"
import itemHandle from "./itemHandle"
import treasureHandle from "./treasureHandle"
import utils from "js/core/utils"

class TaskHandle {

    //装备
    creatTaskEquipment(player: PlayerModel, index, random) {
        if (index == 1) {//史诗 长枪x1
            let equipment = equipmentHandle.create(player, 172011, 2, random, 5)
            return equipment
        }
        if (index == 2) {//史诗 头盔x1
            let equipment = equipmentHandle.create(player, 172051, 3, random, 5)
            return equipment
        }
        if (index == 3) {//史诗 铠甲x1
            let equipment = equipmentHandle.create(player, 172061, 4, random, 5)
            return equipment
        }
        if (index == 4) {//史诗 护盾x1
            let equipment = equipmentHandle.create(player, 172101, 5, random, 5)
            return equipment
        }
        if (index == 5) {//史诗 手套x1
            let equipment = equipmentHandle.create(player, 172031, 6, random, 5)
            return equipment
        }
        if (index == 6) {//史诗 靴子x1
            let equipment = equipmentHandle.create(player, 172092, 7, random, 5)
            return equipment
        }
    }

    //佣兵
    createTaskPartner(player: PlayerModel, quality, random) {
        let partner = { tid: 0, quality }
        let partnerEntries = Object.entries(gameConfig.partners)
        let partnerConfig = utils.getOneFromArray(partnerEntries, random)
        partner.tid = Number(partnerConfig[0])
        return partner
    }

    //装备
    creatTaskTreasure(player: PlayerModel, index, random) {
        if (index == 1) {
            let treasure = treasureHandle.createTreasure(173001, 10, random, 3)
            return treasure
        } 
        else if (index == 2) {
            let treasure = treasureHandle.createTreasure(173002, 10, random, 3)
            return treasure
        }
        else if (index == 3) {
            let treasure = treasureHandle.createTreasure(173003, 10, random, 3)
            return treasure
        }
        else if (index == 4) {
            let treasure = treasureHandle.createTreasure(173004, 10, random, 3)
            return treasure
        }
        else if (index == 101) {
            let treasure = treasureHandle.createTreasure(173009, 1, random, 4, CreateKind.Boss)
            return treasure
        }
        else if (index == 102) {
            let treasure = treasureHandle.createTreasure(173010, 1, random, 4, CreateKind.Boss)
            return treasure
        }
        else if (index == 103) {
            let treasure = treasureHandle.createTreasure(173011, 1, random, 4, CreateKind.Boss)
            return treasure
        }
        else if (index == 104) {
            let treasure = treasureHandle.createTreasure(173012, 1, random, 4, CreateKind.Boss)
            return treasure
        }
    }

    //材料
    creatTaskItem(index) {
        if (index == 1) {//元气丹x10
            return itemHandle.create({ tID: 170004, stack: 10 }, new Random())
        }
        if (index == 2) {//陨铁x2
            return itemHandle.create({ tID: 170005, stack: 2 }, new Random())
        }
        if (index == 3) {//魂石x1
            return itemHandle.create({ tID: 170006, stack: 1 }, new Random())
        }
        if (index == 4) {//八卦残片x20
            return itemHandle.create({ tID: 170016, stack: 20 }, new Random())
        }
        if (index == 5) {//玄晶x1
            return itemHandle.create({ tID: 170010, stack: 1 }, new Random())
        }
        if (index == 6) {//洗髓丹x1
            return itemHandle.create({ tID: 170011, stack: 1 }, new Random())
        }
        if (index == 7) {//元气丹x10
            return itemHandle.create({ tID: 170004, stack: 10 }, new Random())
        }
        if (index == 8) {//元气丹x20
            return itemHandle.create({ tID: 170004, stack: 20 }, new Random())
        }
        if (index == 9) {//元气丹x30
            return itemHandle.create({ tID: 170004, stack: 30 }, new Random())
        }
        if (index == 10) {//陨铁x10
            return itemHandle.create({ tID: 170005, stack: 10 }, new Random())
        }
        if (index == 11) {//陨铁x20
            return itemHandle.create({ tID: 170005, stack: 20 }, new Random())
        }
        if (index == 12) {//陨铁x30
            return itemHandle.create({ tID: 170005, stack: 30 }, new Random())
        }
        if (index == 13) {//陨铁x40
            return itemHandle.create({ tID: 170005, stack: 40 }, new Random())
        }
        if (index == 14) {//魂石x5
            return itemHandle.create({ tID: 170006, stack: 5 }, new Random())
        }
        if (index == 15) {//魂石x10
            return itemHandle.create({ tID: 170006, stack: 10 }, new Random())
        }
        if (index == 16) {//魂石x15
            return itemHandle.create({ tID: 170006, stack: 15 }, new Random())
        }
        if (index == 17) {//魂石x20
            return itemHandle.create({ tID: 170006, stack: 20 }, new Random())
        }
        if (index == 18) {//城镇基石x20
            return itemHandle.create({ tID: 170008, stack: 20 }, new Random())
        }
    }
    //宝石
    creatTaskGem(index) {
        if (index == 1) {//3级红宝石x1
            return itemHandle.create({ tID: 175001, stack: 1, hp: 3 }, new Random())
        }
        if (index == 2) {//3级蓝宝石x1
            return itemHandle.create({ tID: 175002, stack: 1, hp: 3 }, new Random())
        }
        if (index == 3) {//3级黄宝石x1
            return itemHandle.create({ tID: 175003, stack: 1, hp: 3 }, new Random())
        }
        if (index == 4) {//3级绿宝石x1
            return itemHandle.create({ tID: 175004, stack: 1, hp: 3 }, new Random())
        }
        if (index == 5) {//3级绿宝石x1
            return itemHandle.create({ tID: 175001, stack: 1, hp: 5 }, new Random())
        }
        if (index == 6) {//3级绿宝石x1
            return itemHandle.create({ tID: 175002, stack: 1, hp: 5 }, new Random())
        }
        if (index == 7) {//3级绿宝石x1
            return itemHandle.create({ tID: 175003, stack: 1, hp: 5 }, new Random())
        }
        if (index == 8) {//3级绿宝石x1
            return itemHandle.create({ tID: 175004, stack: 1, hp: 5 }, new Random())
        }
    }

    totalTaskAmount() {
        let taskList = gameConfig.task.list
        let amount = 0
        for (let i = 0; i < taskList.length; i++) {
            let taskConfig = gameConfigHandle.configByID[taskList[i]]
            amount += taskConfig.condition.value.length
        }
        return amount
    }
    //是否有任务奖励可以领取
    canGetReward(player: PlayerModel) {
        let taskList = gameConfigHandle.gameConfig.task.list
        let canGet = false
        for (let i = 0; i < taskList.length; i++) {
            if (this.isTaskPass(player, taskList[i])) {
                canGet = true
                break
            }
        }
        return canGet
    }

    isTaskPass(player: PlayerModel, taskID: number) {
        let taskConfig = configByID[taskID]
        let index = -1
        if (player.task.list[taskID] !== undefined && player.task.list[taskID] !== null) {
            index = player.task.list[taskID]
        }
        let values: number[] = taskConfig.condition.value

        //如果该任务已经全部完成
        if (index >= values.length - 1) {
            return false
        }

        //是否达成领奖条件
        let conditionValue = values[index + 1]
        let type = taskConfig.condition.type
        if (type == ConditionType.PartnerLvl) {
            let pLvl = 0
            player.teamList.list.forEach(p => {
                if (p && p.lvl > pLvl) {
                    pLvl = p.lvl
                }
            })
            if (pLvl >= conditionValue) {
                return true
            }
            return false
        } else if (type == ConditionType.AdventureFloor) {
            let nowMapID = player.adventureInfo.mapID
            if (nowMapID > conditionValue) {
                return true
            }
            return false
        } else if (type == ConditionType.EquipmentStr) {
            let isPass = false
            for (let i = 0; i < player.teamList.list.length; i++) {
                let p = player.teamList.list[i];
                if (p) {
                    let equipmentsOn: EquipmentsModel = p.equipments[p.equipmentActive]
                    let equipments = Object.entries(equipmentsOn)
                    let matchCount = 0
                    for (let j = 0; j < equipments.length; j++) {
                        let eq = equipments[j];
                        let eqItem = eq[1]
                        if (eqItem && eqItem.equipment.strengthen >= conditionValue) {
                            matchCount += 1
                        }
                    }
                    if (matchCount >= 6) {
                        isPass = true
                        break
                    }
                }
            }
            return isPass
        } else if (type == ConditionType.Deep) {
            if (taskConfig.condition.times) {
                let times = player.deepInfo.totalTimes
                if (times >= conditionValue) {
                    return true
                }
                return false
            } else {
                let progress = player.deepInfo.progress
                if (progress >= conditionValue) {
                    return true
                }
                return false
            }
        } else if (type == ConditionType.BattlePower) {
            let power = player.baseInfo.maxPower
            if (power >= conditionValue) {
                return true
            }
            return false
        } else if (type == ConditionType.MarketTimes) {
            let buyTimes = player.market.buyTimes
            if (buyTimes >= conditionValue) {
                return true
            }
            return false
        } else if (type == ConditionType.WorldBoss) {
            if (taskConfig.condition.times) {
                let times = player.wbInfo.totalTimes
                if (times >= conditionValue) {
                    return true
                }
                return false
            } else {
                let damage = player.wbInfo.progress
                if (damage >= conditionValue) {
                    return true
                }
                return false
            }
        } else if (type == ConditionType.Bar) {
            if (taskConfig.condition.times) {
                let buyTimes = player.baseInfo.barRecuritTimes
                if (buyTimes >= conditionValue) {
                    return true
                }
            } else {
                let refreshTimes = player.baseInfo.totalRefreshBarTimes
                if (refreshTimes >= conditionValue) {
                    return true
                }
            }
            return false
        }
    }

    calcCampTaskFinalReward(player: PlayerModel, reward: any, partnerList: string[]) {
        let value = reward.value
        let rate = 0
        partnerList = partnerList || []
        // 计算等级加成 = 佣兵等级 / 10 
        let lvlRate = 0
        // 计算星级加成 = 佣兵星级
        let starRate = 0
        // 计算品质加成 = 佣兵品质
        let qualityRate = 0
        partnerList.filter(p => p).forEach(p => {
            let partner = player.teamList.camp.find(pn => pn && pn.index == p)
            if (partner) {
                lvlRate += Math.floor(partner.lvl / 10)
                qualityRate += Math.floor(partner.quality)
                starRate += Math.floor(partner.star)
            }
        })
        rate = lvlRate + starRate + qualityRate
        value += Math.floor(rate / 100 * value)
        return { value, rate }
    }
}

let taskHandle = new TaskHandle()

export default taskHandle